package com.situ.dao.impl;

import com.situ.common.Global;
import com.situ.dao.MovieDao;
import com.situ.dao.UserDao;
import com.situ.pojo.Movie;
import com.situ.pojo.Student;
import com.situ.pojo.search.MovieDTO;
import com.situ.util.Pagination;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class MovieDaoImpl implements MovieDao {
    //引入行映射器
    private BeanPropertyRowMapper<Movie> rowMapper = new BeanPropertyRowMapper<>(Movie.class) ;
    @Override
    public List<Movie> list(MovieDTO movieDTO, Pagination pagination) {
        JdbcTemplate jdbcTemplate = Global.getJdbcTemplate();
        List<Object> args=new ArrayList<>();

        String select="select id,name,date_time,rating,actor,money,movie_time from t_movie";
        StringBuilder sb = new StringBuilder();
        sb.append(" where 1=1");
        if (StringUtils.hasText(movieDTO.getName())){
            sb.append(" and name like ?");
            args.add("%"+movieDTO.getName()+"%");
        }
        if (movieDTO.getMovieTime()!=null&&movieDTO.getMovieTime()>0){
            sb.append(" and movie_time >= ?");
            args.add(movieDTO.getMovieTime());
        }

        if (StringUtils.hasText(movieDTO.getActor())){
            sb.append(" and actor like ?");
            args.add("%"+movieDTO.getActor()+"%");
        }
        if (movieDTO.getStartTime() != null){
            sb.append(" and date_time >= ?");
            args.add(movieDTO.getStartTime());
        }
        if (movieDTO.getEndTime() != null){
            sb.append(" and date_time < ?");
            args.add(movieDTO.getEndTime());
        }
        if (movieDTO.getRating() != null){
            sb.append(" and rating >= ?");
            args.add(movieDTO.getRating());
        }


        //查询记录总数
        String countSql = "select count(0) from ("+select+sb.toString()+") t";
        Long count=jdbcTemplate.queryForObject(countSql, Long.class, args.toArray());

        pagination.setTotal(count);
        //是否排序
        if (StringUtils.hasText(movieDTO.getOrder())){
            sb.append(" order by money "+movieDTO.getOrder());
        }

        String limitsql = select+sb.toString()+" limit  ?,?";

        //查询总票房
        pagination.setTotalMoney(jdbcTemplate.queryForObject("select sum(money) from ("+select+sb.toString()+") t", Double.class, args.toArray()));

        args.add(pagination.getOffset());
        args.add(pagination.getLimit());
        List<Movie> movieList = jdbcTemplate.query(limitsql, rowMapper,args.toArray());
        return movieList;
    }

    @Override
    public int add(Movie movie) {
        JdbcTemplate jdbcTemplate = Global.getJdbcTemplate();
        return jdbcTemplate.update("insert into t_movie(name,date_time,rating,actor,money,movie_time) values(?,?,?,?,?,?)",
                movie.getName(),movie.getDateTime(),movie.getRating(),movie.getActor(),movie.getMoney(),movie.getMovieTime());
    }

    @Override
    public Movie findById(Integer integer) {
        JdbcTemplate jdbcTemplate = Global.getJdbcTemplate();
        return jdbcTemplate.queryForObject("select id,name,date_time,rating,actor,money,movie_time from t_movie where id = ?", rowMapper, integer);
    }

    @Override
    public int edit(Movie movie) {
        JdbcTemplate jdbcTemplate = Global.getJdbcTemplate();
        return jdbcTemplate.update("update t_movie set name = ?,date_time = ?,rating = ?,actor = ?,money = ?,movie_time = ? where id = ?",
                movie.getName(),movie.getDateTime(),movie.getRating(),movie.getActor(),movie.getMoney(),movie.getMovieTime(),movie.getId());

    }

    @Override
    public int deleteByIds(List<Integer> integers) {
        JdbcTemplate jdbcTemplate = Global.getJdbcTemplate();
        if (integers == null || integers.isEmpty()) {
            return -1;
        }
        String sql = "delete from t_movie where id in(";
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < integers.size(); i++) {
            sql += "?,";
        }
        sql = sql.substring(0, sql.length() - 1) + ")";
        return jdbcTemplate.update(sql, integers.toArray());
    }
}
